package com.hhsjdesign.test.act;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.hhsjdesign.headerpullrefresh.R;
import com.hhsjdesign.test.BaseFragmentAdapter;
import com.hhsjdesign.test.ListFragment;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerSample extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_home);
        setupViewPager();
    }

    private void setupViewPager() {

        // 第一步，初始化ViewPager和TabLayout
        ViewPager mViewPager = findViewById(R.id.viewpager);
        TabLayout mTabLayout = findViewById(R.id.tabs);
        List<Fragment> mFragments;
        String[] mTitles = new String[]{
                "主页", "微博", "相册"
        };
        mFragments = new ArrayList<>();
        mFragments.add(ListFragment.newInstance(mTitles[0], 30));
        mFragments.add(ListFragment.newInstance(mTitles[1], 50));
        mFragments.add(ListFragment.newInstance(mTitles[2], 80));
        // 第二步：为ViewPager设置适配器
        BaseFragmentAdapter adapter = new BaseFragmentAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //  第三步：将ViewPager与TableLayout 绑定在一起
        mTabLayout.setupWithViewPager(mViewPager);
    }


}